<template>
  <div class="dashboard-container">
    <div class="app-container">
      <abc>
        <template #after>
          <el-button type="primary" size="small" @click="add('1','0')">添加权限</el-button>
        </template>
      </abc>
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        >
        <el-table-column
          prop="name"
          label="权限名称"
        />
        <el-table-column
          prop="code"
          label="权限标识"
        />
        <el-table-column
          prop="description"
          label="权限描述"
        />
        <el-table-column
          label="操作"
        >
          <template #default="{row}">
            <el-button type="text" size="small" :disabled="row.type!==1" @click="add(2,row.id)">添加</el-button>
            <el-button type="text" size="small" @click="edit(row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="deletes(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="`${showText}权限点`" :visible="dialogshow" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import abc from '@/components/page.vue'
import { pro } from '@/views/departments/comPonents/prope'
import { getpermisson, depermisson, addPermission, updatePermission, getPermissionDetail } from '@/api/permisson'
export default {
  components: {
    abc
  },
  data() {
    return {
      tableData: [],
      dialogshow: false,
      formData: {
        type: '',
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      },
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getpermisson()
  },
  methods: {
    async getpermisson() {
      const res = await getpermisson()
      this.tableData = pro(res, '0')
      console.log(this.tableData, 4889)
    },
    async add(a, b) {
      this.dialogshow = true
      this.formData.type = a
      this.formData.pid = b
      // this.formData.id=
    },
    async edit(id) {
      this.dialogshow = true
      this.formData = await getPermissionDetail(id)
    },
    btnCancel() {
      this.formData = {}
      this.dialogshow = false
    },
    btnOK() {
      try {
        if (this.formData.id) {
          updatePermission(this.formData)
        } else {
          addPermission({ ...this.formData, id: this.formData.pid })
        }
      } catch {
        console.log(new Error())
      }
      this.$message.success('操作成功')
      this.getpermisson()
      this.dialogshow = false
    },
    async deletes(id) {
      console.log(id)
      try {
        await this.$confirm('确定要删除？')
        await depermisson(id)
        this.$message('已删除')
        this.getpermisson()
      } catch {
        console.log(new Error())
      }
    }
  }
}
</script>

<style>

</style>
